<template>
  <div class='dialogadd'>
    <el-dialog
      :title="`${data.id ? '修改' : '新增'}目录`"
      :visible.sync="showDialog"
      width="400px"
    >
      <el-form
        ref="form"
        size="small"
        label-width="80px"
        :model="formModel"
        :rules="formRules"
      >
        <el-form-item
          label="所属学科"
          v-if="!$route.query.id"
          prop="subjectID"
        >
          <el-select
            v-model="formModel.subjectID"
            placeholder="请选择"
            style="width: 100%"
          >
            <el-option
              v-for="item in subjectOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="目录名称"
          prop="directoryName"
        >
          <el-input
            placeholder="请输入目录名称"
            v-model="formModel.directoryName"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="showDialog = false">取消</el-button>
        <el-button
          @click="btnOK()"
          type="primary"
        >确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update } from '@/api/hmmm/directorys.js'
import { simple as simpleList } from '@/api/hmmm/subjects'

export default {
  name: 'dialogAdd',
  props: {
    data: {
      dafault: () => { },
      type: Object
    }
  },
  data () {
    return {
      showDialog: false,
      formModel: {
        id: null, // 目录ID
        subjectID: null, // 学科ID
        directoryName: null // 目录名称
      },
      formRules: {
        subjectID: [{ type: 'number', message: '请选择所属学科', tirgger: 'blur' }],
        directoryName: [{ required: true, message: '请输入标签名称', tirgger: 'blur' }]
      },
      subjectOptions: []
    }
  },

  async created () {
    // 所属学科的下拉菜单
    const { data: subjectArr } = await simpleList()
    this.subjectOptions = subjectArr
  },
  methods: {
    open () {
      // 打开
      this.showDialog = true
      // 判断是从学科处进来打开弹窗还是左菜单进来打开弹窗
      if (this.data.id) {
        // 从左菜单进来
        const { id, subjectID, directoryName } = this.data
        this.formModel = { id, subjectID, directoryName }
      } else {
        // 从学科进来
        this.formModel = {
          id: null,
          subjectID: +this.$route.query.id || null,
          simpleName: null
        }
        // 清除校验效果
        this.$nextTick(() => {
          this.$refs.form.clearValidate()
        })
      }
    },
    // 确定按钮
    async btnOK () {
      this.$refs.form.validate(async (value) => {
        if (value) {
          // 提交
          if (!this.data.id) {
            // 新增数据
            await add(this.formModel)
          } else {
            // 修改
            this.formModel.id = this.data.id
            await update(this.formModel)
          }
          // 提示
          this.$message.success('操作成功')
          // 关闭弹窗并刷新页面
          this.showDialog = false
          this.$emit('updateList')
        }
      })
    }
  }
}
</script>

<style scoped lang='scss'></style>
